#ifndef __myUARTh
#define __myUARTh

#include "lpc17xx_i2c.h"
#include "vtUtilities.h"
#include "FreeRTOS.h"
#include "projDefs.h"
#include "semphr.h"

#define UARTErrInit -1
#define UARTInitSuccess 0

#define HEADER_DATA_BYTE 0xFF

// States for UART handling
#define UART_READ_HEADER_BYTE       0x01
#define UART_READ_TYPE_BYTE         0x02
#define UART_READ_DATA_BYTES        0x03
#define UART_VALIDATE_CHECKSUM_BYTE 0x04
 
#define UARTMLen 6

typedef struct __UARTStruct {
	uint8_t devNum;	  						// Number of the I2C peripheral (0,1,2 on the 1768)
	LPC_UART_TypeDef *devAddr;	 			// Memory address of the I2C peripheral
	unsigned portBASE_TYPE taskPriority;   	// Priority of the I2C task
	xSemaphoreHandle binSemaphore;		   	// Semaphore used between I2C task and I2C interrupt handler
	xQueueHandle inQ;					   	// Queue used to send messages from other tasks to the I2C task
	xQueueHandle outQ;						// Queue used by the I2C task to send out results
	uint8_t recvBuf[UARTMLen];
	uint8_t recvCount;
	uint8_t state;
} UARTStruct;

typedef struct __UARTMsg {
	uint8_t msgType; // A field you will likely use in your communications between processors (and for debugging)
	uint8_t slvAddr; // Address of the device to whom the message is being sent (or was sent)
	uint8_t	rxLen;	 // Length of the message you *expect* to receive (or, on the way back, the length that *was* received)
	uint8_t txLen;   // Length of the message you want to sent (or, on the way back, the length that *was* sent)
	uint8_t status;  // status of the completed operation -- I've not done anything much here, you probably should...
	uint8_t buf[UARTMLen]; // On the way in, message to be sent, on the way out, message received (if any)
} UARTMsg;

int myUARTInit(UARTStruct *devPtr,uint8_t devNum,unsigned portBASE_TYPE taskPriority,uint32_t uartBaudRate);

void sendUART(uint8_t* buffer, uint8_t buflen);

portBASE_TYPE UARTEnQ(UARTStruct *dev,uint8_t msgType,uint8_t slvAddr,uint8_t txLen,const uint8_t *txBuf,uint8_t rxLen);


portBASE_TYPE UARTDeQ(UARTStruct *dev,uint8_t maxRxLen,uint8_t *rxBuf,uint8_t *rxLen,uint8_t *msgType,uint8_t *status);


#endif